package com.selenium.selenium3;

import java.io.File;
import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

import com.selenium.utils;




public class chrome {

	

	
	
	
	
//	//判断元素是否存在
	
	public static boolean isPresentEle(By by, WebDriver driver) {
		try {
			driver.findElement(by);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	
	//截图
//	public static void snapshot(TakesScreenshot drivername, String filename)
//	  {
//	      // this method will take screen shot ,require two parameters ,one is driver name, another is file name
//	      
//	    String currentPath = System.getProperty("user.dir"); //get current work folder
//	    System.out.println(currentPath);
//	    File scrFile = drivername.getScreenshotAs(OutputType.FILE);
//	        // Now you can do whatever you need to do with it, for example copy somewhere
//	        try {
//	            System.out.println("save snapshot path is:"+currentPath+"/"+filename);
//	            FileUtils.copyFile(scrFile, new File(currentPath+"\\"+filename));
//	        } catch (IOException e) {
//	            // TODO Auto-generated catch block
//	            System.out.println("Can't save screenshot");
//	            e.printStackTrace();
//	        } 
//	        finally
//	        {
//	           
//	            System.out.println("screen shot finished");
//	        }
//	  }
	
	/**
	 * 获取当前时间，并转换为字符串
	 * @return
	 */
	
	private static String Date2String(){
		 Format format = new SimpleDateFormat("yyyyMMdd-HHmmss");
	     return format.format(new Date());
	
	}
	
	/**
	 * 自动截屏
	 * @param driver
	 */

	private static void takeScreenshot(WebDriver driver){
        try {
        	//获取截图file
        	File srcFile= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        	//新文件
        	File targeFile = new File("errorScreenshot/" + Date2String()+".png");
        	
            //将图片移动到指定位置
            FileUtils.copyFile(srcFile, targeFile);
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }
	
	
	
	
	
	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub

		
		
		System.setProperty("webdriver.chrome.driver", "D:\\soft\\test\\selenium3\\driver\\chromedriver.exe");
		WebDriver wd=new ChromeDriver();
		//wd.get("http://www.qq.com");
		wd.get("http://www.baidu.com");
		wd.manage().window().maximize();
		
		 utils util = new utils(); 
		if(util.isPresentEle(By.id("kw1"), wd)) {
			
			//if(isPresentEle(By.id("kw"), wd)) {
			System.out.println("找到元素");
			WebElement text= wd.findElement(By.id("kw"));
			

			WebElement ty = wd.findElement(By.id("kw"));
			String color=ty.getCssValue("color");
			System.out.println("type:"+ty.getAttribute("name"));
			System.out.println("color:"+color);	
			text.sendKeys("css");
			text.submit();
			
			
		
		//wd.navigate().to("http://www.baidu.com");
//		wd.quit();
		
		


 

		
		
		//wd.manage().window().setSize(new Dimension(900, 500));
		
		
		//JavascriptExecutor js=(JavascriptExecutor) wd;
		
		//js.executeScript("window.scrollTo(0,document.body.scrollHeight);");
		
		//js.executeScript("window.scroll(0,document.body.scrollHeight)");
		//将页面滚动条拖到底部
	    //((JavascriptExecutor)wd).executeScript("window.scrollTo(0,document.body.scrollHeight);");
		
		
		
		wd.findElement(By.linkText("新闻")).click();
		wd.navigate().back();
		wd.navigate().forward();
		System.out.printf("back to %s \n", wd.getCurrentUrl());
	    
		System.out.println("forward");
		wd.navigate().forward();
		System.out.printf("back to %s \n", wd.getCurrentUrl());
	    //Thread.sleep(5000);
	    
		System.out.println("refresh");
		//wd.navigate().refresh();
		
		for(int i=0;i<2;i++){
			System.out.println("i="+i);
			
			Thread.sleep(5000);
			
			wd.navigate().forward();
			
			//System.out.println("5秒后，quit");
			//Thread.sleep(20000);
			//wd.quit();
		}
	    
		System.out.println("5秒后，quit");
		
		//wd.close();
		wd.quit();
	   
	}
	
		else {
			System.out.println("对象不能找到\n");
			Thread.sleep(5000);
			//takeScreenshot((TakesScreenshot)wd,"open_bake.png");

			//设置截图
				takeScreenshot(wd);
				
				System.out.println("截图保存成功\n");
				System.out.println("data is :"+Date2String());
			}
			
			
			//wd.close();
			wd.quit();
		}

	}	

		
